package com.ruoyi.washer.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.washer.domain.XwDevice;
import com.ruoyi.washer.domain.XwEnduser;
import com.ruoyi.washer.mapper.XwDeviceMapper;
import com.ruoyi.washer.mapper.XwDivideAccountsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.washer.mapper.XwAgentMapper;
import com.ruoyi.washer.domain.XwAgent;
import com.ruoyi.washer.service.IXwAgentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 代理商Service业务层处理
 *
 * @author hawking
 * @date 2023-12-15
 */
@Service
public class XwAgentServiceImpl implements IXwAgentService
{
    @Autowired
    private XwAgentMapper xwAgentMapper;

    @Autowired
    private XwDeviceMapper xwDeviceMapper;

    @Autowired
    private XwDivideAccountsMapper xwDivideAccountsMapper;

    /**
     * 查询代理商
     *
     * @param agentId 代理商主键
     * @return 代理商
     */
    @Override
    public XwAgent selectXwAgentByAgentId(Long agentId)
    {
        return xwAgentMapper.selectXwAgentByAgentId(agentId);
    }

    /**
     * 查询代理商列表
     *
     * @param xwAgent 代理商
     * @return 代理商
     */
    @Override
    public List<XwAgent> selectXwAgentList(XwAgent xwAgent)
    {
        return xwAgentMapper.selectXwAgentList(xwAgent);
    }

    /**
     * 新增代理商
     *
     * @param xwAgent 代理商
     * @return 结果
     */
    @Override
    public int insertXwAgent(XwAgent xwAgent)
    {
        return xwAgentMapper.insertXwAgent(xwAgent);
    }

    /**
     * 修改代理商
     *
     * @param xwAgent 代理商
     * @return 结果
     */
    @Override
    public int updateXwAgent(XwAgent xwAgent)
    {
        return xwAgentMapper.updateXwAgent(xwAgent);
    }

    /**
     * 批量删除代理商
     *
     * @param agentIds 需要删除的代理商主键
     * @return 结果
     */
    @Override
    public int deleteXwAgentByAgentIds(Long[] agentIds)
    {
        return xwAgentMapper.deleteXwAgentByAgentIds(agentIds);
    }

    /**
     * 删除代理商信息
     *
     * @param agentId 代理商主键
     * @return 结果
     */
    @Override
    public int deleteXwAgentByAgentId(Long agentId)
    {
        return xwAgentMapper.deleteXwAgentByAgentId(agentId);
    }

    public XwAgent getAgentMessage(String openId){
        return xwAgentMapper.selectXwAgentByOpenId(openId);
    }

    @Transactional
    public int setAgentBingMsg(String agentName,String agentPhone,String activationCode,String openId){
        XwAgent xwAgent=xwAgentMapper.selectXwAgentByOpenId(openId);
        if(xwAgent!=null){
            return 1;//当前登录用户已经绑定过
        }
        xwAgent=xwAgentMapper.selectXwAgentByBingMsg(agentName,agentPhone,activationCode);
        if(xwAgent!=null){
            String openId01=xwAgent.getAgentOpenId();
            if(openId01!=null&&openId01.length()>5){
                return 2;//填写的信息已经绑定过
            }else{
                xwAgent.setAgentOpenId(openId);
                xwAgent.setUpdatedTime(new Date());
                xwAgent.setBindState("Y");
                xwAgentMapper.updateXwAgent(xwAgent);
                return 3;//绑定成功
            }
        }
        return 4;//系统找不到对应的信息
    }
    @Transactional
    public int setAgentBingMsg(XwAgent xwAgent){
        XwAgent xwAgent01=xwAgentMapper.selectXwAgentByOpenId(xwAgent.getAgentOpenId());
        if(xwAgent01!=null){
            return 1;//当前登录用户已经申请过
        }
        xwAgent.setBindState("N");
        xwAgent.setFreezeState("N");
        xwAgent.setCreatedTime(new Date());
        xwAgent.setCashoutType("1");
        xwAgentMapper.insertXwAgent(xwAgent);
        return 2;
    }

   public JSONObject getAgentDeviceList(String openId){
        JSONObject jsonObject=new JSONObject();
       XwDevice xwDevice=new XwDevice();
       xwDevice.setAgentOpenId(openId);
       List<XwDevice> xwDeviceList= xwDeviceMapper.selectXwDeviceListAgent(xwDevice);
       jsonObject.put("xwDeviceList",xwDeviceList);
       if(xwDeviceList!=null&&xwDeviceList.size()>0){
                int b4G=0;
                int bluetooth=0;
                int allCount=xwDeviceList.size();
                for(int i=0;i<allCount;i++){
                    XwDevice xwDevice1=xwDeviceList.get(i);
                    if(xwDevice1.getDeviceType().equals("1")){
                        b4G++;
                    }else{
                        bluetooth++;
                    }
                }
           jsonObject.put("b4G", b4G);
           jsonObject.put("bluetooth", bluetooth);
           jsonObject.put("allCount", allCount);
           String currentMonth = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
           jsonObject.put("currentMonthDivideAmount", xwDivideAccountsMapper.getMonthDivideAmountByOpenIdAndDivideState(currentMonth, openId, "Y"));
           String previousMonth = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
           jsonObject.put("previousMonthDivideAmount", xwDivideAccountsMapper.getMonthDivideAmountByOpenIdAndDivideState(previousMonth, openId, "Y"));
       } else {
           jsonObject.put("b4G", 0);
           jsonObject.put("bluetooth", 0);
           jsonObject.put("allCount", 0);
           jsonObject.put("currentMonthDivideAmount", 0);
           jsonObject.put("previousMonthDivideAmount", 0);
       }
       return jsonObject;
   }
    public int saveAgentCashoutSettings(String cashoutType,
                                          String agentMerchantCode,
                                          String agentMerchantName,
                                          String openId){
        XwAgent xwAgent=xwAgentMapper.selectXwAgentByOpenId(openId);
        xwAgent.setCashoutType(cashoutType);
        xwAgent.setAgentMerchantCode(agentMerchantCode);
        xwAgent.setAgentMerchantName(agentMerchantName);
        return xwAgentMapper.updateXwAgent(xwAgent);
    }
}
